package com.pdstudio.carrecom.ui.activity.myclub;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.http.RequestParams;
import com.pdstudio.carrecom.R;
import com.pdstudio.carrecom.api.HttpExecuteJson;
import com.pdstudio.carrecom.api.ServiceHelper;
import com.pdstudio.carrecom.api.HttpExecuteJson.httpReturnJson;
import com.pdstudio.carrecom.app.AppContext;
import com.pdstudio.carrecom.app.UIHelper;
import com.pdstudio.carrecom.bean.LoginInfo;
import com.pdstudio.carrecom.bean.NewActivity;
import com.pdstudio.carrecom.bean.ResultInfo;
import com.pdstudio.carrecom.tools.StringUtils;
import com.pdstudio.carrecom.ui.activity.myclub.ActivityNewActivity.MyOnPageChangeListener;
import com.pdstudio.carrecom.ui.adapter.ImagePagerAdapter;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 最新活动报名
 * 类: ActivityNewActivitySignal <br>
 * 描述: TODO <br>
 * 作者: xiangzhaoyu  <br>
 * 时间: 2015-6-21 下午8:57:54
 */
public class ActivityNewActivitySignal extends Activity {
	

	private View customLiveIndexTitleView;
	private TextView txtTitle;
	private ImageView ivBack;
	
	private EditText mUserName,mPhone;
	private Button mSubmit;
	
	private NewActivity currenta=null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_myclub_signal);
		
		currenta=(NewActivity)getIntent().getExtras().getSerializable("act");

		initTitle();
		InitialView();
	}
	
	private void initTitle() {
		txtTitle = (TextView) findViewById(R.id.title_text_nav);

		txtTitle.setText("我要报名");

		ivBack = (ImageView) findViewById(R.id.title_back);
		ivBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();				
			}
		});
	}
	
	private void InitialView(){
		
		mUserName=(EditText)findViewById(R.id.et_mc_username);
		mPhone=(EditText)findViewById(R.id.et_mc_phone);
		mSubmit=(Button)findViewById(R.id.btn_mp_submit);
		mSubmit.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(mUserName.getText().equals("")||mPhone.getText().equals("")){
					UIHelper.ToastMessage(ActivityNewActivitySignal.this, "用户名或者手机号不能为空！");
				}else{
					try {
						HttpExecuteJson http = new HttpExecuteJson(ActivityNewActivitySignal.this, mSignalListener);
						RequestParams rps=new RequestParams();
						rps.addBodyParameter("Uid", AppContext.getInstance()._userId+"");
						rps.addBodyParameter("attachsId", currenta.id+"");
						rps.addBodyParameter("name", mUserName.getText().toString());
						rps.addBodyParameter("tel",mPhone.getText().toString());
						http.post(ServiceHelper.NewActivitySigUp, rps);
					} catch (Exception e) {
						e.printStackTrace();
						UIHelper.ToastMessage(ActivityNewActivitySignal.this, "报名失败："+e.getMessage());
					}
				}
			}});
		
	}
	
	private void DoJsonRp(String json){
		ResultInfo edv = null;
		try {
			edv = new Gson().fromJson(json, new TypeToken<ResultInfo>() {
			}.getType());
			if ((edv != null)
					&& (edv.msg.equals(ServiceHelper.msg_code_sucess))) {
				// 原本在initview的代码放在这下面
				try {
					UIHelper.ToastMessage(ActivityNewActivitySignal.this, "报名成功！");
					finish();
				} catch (Exception e) {
					e.printStackTrace();
					UIHelper.ToastMessage(ActivityNewActivitySignal.this, "报名失败！"+e.getMessage());
				}
			} else {
				UIHelper.ToastMessage(ActivityNewActivitySignal.this, "报名失败！"+edv.msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			UIHelper.ToastMessage(ActivityNewActivitySignal.this, "报名失败！"+e.getMessage());
		}
	}
	
	private httpReturnJson mSignalListener = new httpReturnJson() {
		@Override
		public void onFailure(int error, String msg) {
			UIHelper.ToastMessage(ActivityNewActivitySignal.this, "报名失败！");
		}

		@Override
		public void onCancel() {
			// TODO Auto-generated method stub
			UIHelper.ToastMessage(ActivityNewActivitySignal.this, "报名失败！");
		}

		// 获取到饲料配方类型成功
		public void onSuccess(String result) {
			// TODO Auto-generated method stub
			// UIHelper.ToastMessage(_context,"总条数=" + result + "");
			DoJsonRp(result);
		}
	};



}
